package com.fitns.user.controller.baseConfig;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fitns.user.service.baseConfig.TbPersonLederService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.fitns.user.conf.Constants;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.TbPersonLederDTO;
import com.fitns.user.dto.baseConfig.TbPersonLederVO;
import com.fitns.user.model.baseConfig.TbPersonLedger;
import com.fitns.user.service.pub.FileService;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.ResultData;
import com.fitns.user.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;
import com.xiaoleilu.hutool.util.CollectionUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("backend")
@Api(description="人员台账")
public class TbPersonLederController {

	@Autowired
	private TbPersonLederService tbPersonLederService;

	@Autowired
	private FileService fileService;
	
	
	@PostMapping("insert/personLeder")
	@ApiOperation("新增人员台账")
	public Object insertPersonLeder(@Validated TbPersonLederDTO tbPersonLederDTO, String phone) {
		int insert =  tbPersonLederService.insertPersonLeder(tbPersonLederDTO,phone);
		Resp resp = null;
		if(insert!=0) {
			resp = Resp.wrap("人员台账添加成功");
		}else {
			 resp = Resp.errorWrap("人员台账添加失败");
		}
		return resp;
	}
	
	@PutMapping("update/personLeder")
	@ApiOperation("修改人员台账")
	public Object updatePersonLeder(@Validated TbPersonLederDTO tbPersonLederDTO) {
		int update =  tbPersonLederService.updatePersonLeder(tbPersonLederDTO);
		Resp resp = null;
		if(update!=0) {
			resp = Resp.wrap("人员台账修改成功");
		}else {
			 resp = Resp.errorWrap("人员台账修改失败");
		}
		return resp;
	}
	
	@DeleteMapping("delete/personLeder")
	@ApiOperation("根据ID删除人员台账")
	public Object deletePersonById(@RequestParam long id) {
		int delete =  tbPersonLederService.deletePersonById(id);
		Resp resp = null;
		if(delete!=0) {
			resp = Resp.wrap("人员台账删除成功");
		}else {
			 resp = Resp.errorWrap("人员台账删除失败");
		}
		return resp;
	}
	@PostMapping("select/personLeder")
	@ApiOperation("查询人员台账")
    public Object selectBannerList(TbPersonLederDTO tbPersonLederDTO) {
        PageInfo<TbPersonLederVO> bannerId = tbPersonLederService.selectBannerList(tbPersonLederDTO);
        bannerId.setPageSize(tbPersonLederDTO.getPageSize());
        return Resp.page(bannerId);
    }
	
	
	@ApiOperation("根据id查询数据类型信息")
	@GetMapping("select/personLederById")
	public Object selectPersonLederById(@RequestParam long id) {
		TbPersonLedger byId =  tbPersonLederService.selectPersonLederById(id);
		return byId;
	}
	
	@PostMapping("updateFile/person")
	@ApiOperation("上传图片并新增或修改相应信息")
	public Integer inserIcon(TbPersonLederDTO tbPersonLederDTO, MultipartFile pic) throws Exception {
		// 把图片保存到图片目录下
		// 为每个文件生成一个新的文件名
		if(Validator.isNotNull(pic)) {
			String picName = UuidUtils.getUUID();
			// 取文件扩展名
			String oriName = pic.getOriginalFilename();
			
			if(Validator.isNotNull(oriName)&&"".equals(oriName)){
				return tbPersonLederService.updatePersonLeder(tbPersonLederDTO);
			}
			String extName = oriName.substring(oriName.lastIndexOf("."));
			// 保存文件
			pic.transferTo(new File(Constants.IMAGE_PATH + picName + extName));
			// 把文件名保存到数据库
			tbPersonLederDTO.setImage(picName + extName);
			Integer icon = tbPersonLederService.inserIcon(tbPersonLederDTO);
			//int insert =  tbFacilityTypeService.insertFacilityType(tbFacilityTypeDTO);
			return icon;
		}
		return tbPersonLederService.updatePersonLeder(tbPersonLederDTO);
	}
	
	
	
	@GetMapping("/downLoadImage")
	@ApiOperation("下载图片")
	public String IoReadImage(String imgName, HttpServletRequest request, HttpServletResponse response) throws IOException {
		return fileService.IoReadImage(imgName, request, response);
	}
	
	
	/** 
	 *  
	 * ################### exclue文件导入，批量导入在service中做了处理，所以sql中新增的sql没有用到foeach循环 #####################
	 * 
	 * */
	@PostMapping("import/person")
	@ApiOperation("exclue导入人员台账")
    public String importData(@RequestParam("file") MultipartFile file) throws Exception {
    	//ExcelUtils.importExcel();   来自于工具类
    	List<List<String>> excelList=ExcelUtils.importExcel(file);
        List<String> headList=excelList.get(0);
        List<TbPersonLederVO> facilityDataVos=new ArrayList<>();
        boolean is = ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.NAME.equals(headList.get(0));
        if(ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.NAME.equals(headList.get(0))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.SEX.equals(headList.get(1))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.ID_CARD.equals(headList.get(2))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.PHONE.equals(headList.get(3))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.ADDRESS.equals(headList.get(4))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.ATTENDANCE_WAY.equals(headList.get(5))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.TERMINAL.equals(headList.get(6))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.SOS.equals(headList.get(7))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.ONE_BUTTON.equals(headList.get(8))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.TWO_BUTTON.equals(headList.get(9))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.THREE_BUTTON.equals(headList.get(10))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.IS_ENTER.equals(headList.get(11))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.SOCIAL_NUM.equals(headList.get(12))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.RECORD_NUM.equals(headList.get(13))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.CONTRACT_NUM.equals(headList.get(14))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.GUARD_TIME.equals(headList.get(15))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.LEAVE_TIME.equals(headList.get(16))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.REMARK.equals(headList.get(17))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.TYPE.equals(headList.get(18))&&
        		ExcelConstants.PERSON_LEDGER_IMPORT_HEADER.IMAGE.equals(headList.get(19))){
        	 facilityDataVos=tbPersonLederService.importExcel(excelList);
        }else {
        	TbPersonLederVO facilityDataVo=new TbPersonLederVO();
             facilityDataVo.setMsg("模板有误，请勿修改模板！");
             facilityDataVos.add(facilityDataVo);
        }

        ResultData resultData=new ResultData();
        String errorData="";
        if (CollectionUtil.isNotEmpty(facilityDataVos)) {
            errorData = JSON.toJSONString(facilityDataVos);
        }
        if(org.apache.commons.lang3.StringUtils.isEmpty(errorData)){
            resultData.setMsg("成功");
        }else{
            resultData.setCode(300);
            resultData.setMsg("失败");
        }

        resultData.setObj(errorData);
        return JSON.toJSONString(resultData);
    }
	/**  
	 * 
	 * ################### exclue文件导入到此为止
	 * 
	 * */
	
	
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////



/** 
*  
* ################### exclue文件导出，也就是从数据库导出文件到exclue
* 
* */
	@GetMapping("export/person")
    @ApiOperation("exclue导出人员台账")
	 public void exportCustomer(TbPersonLederDTO tbPersonLederDTO, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<List<String>> excelList =tbPersonLederService.export(tbPersonLederDTO);
        ExcelUtils.export(response,request,"人员台账",excelList);
    }
	
}
